package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateAssessFunctionDTO;
import com.itlong.cloud.POJO.PO.AssessFunctionPO;
import com.itlong.cloud.POJO.VO.operate.OperateAssessFunctionVO;
import com.itlong.cloud.POJO.VO.operate.OperateGetRoleMenuVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IOperateAssessFunctionDao;
import com.itlong.cloud.operate.service.IOperateAssessFunctionService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <desc>
 *      运营-报价系统资源菜单业务处理实现类
 * </desc>
 *
 * @createDate2020/6/8
 **/
@Service
public class OperateAssessFunctionServiceImpl implements IOperateAssessFunctionService {

    @Autowired
    private IOperateAssessFunctionDao iOperateAssessFunctionDao;

    /**
     * <desc>
     *      分页获取报价菜单
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价菜单DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/9
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateAssessFunctionVO> getPage(OperateAssessFunctionDTO operateAssessFunctionDTO) throws Exception {
        Page<OperateAssessFunctionVO> page = new Page<>();
        page.getPage().setCurrentPage(operateAssessFunctionDTO.getCurrentPage());
        page.getPage().setPageSize(operateAssessFunctionDTO.getPageSize());
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateAssessFunctionDTO);
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        page.setRows(iOperateAssessFunctionDao.getList(params),iOperateAssessFunctionDao.getListCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取单个报价菜单
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价菜单DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/9
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateAssessFunctionVO getOne(OperateAssessFunctionDTO operateAssessFunctionDTO) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("functionId",operateAssessFunctionDTO.getFunctionId());
        return iOperateAssessFunctionDao.getOne(params);
    }

    /**
     * <desc>
     *      获取子节点
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价菜单DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/9
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<OperateAssessFunctionVO> getChild(OperateAssessFunctionDTO operateAssessFunctionDTO) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("functionId",operateAssessFunctionDTO.getFunctionId());
        params.put("type",1);
        List<OperateAssessFunctionVO> roots=iOperateAssessFunctionDao.getChild(operateAssessFunctionDTO.getFunctionId(),1);
        roots.forEach(child->{
            List<OperateAssessFunctionVO> list = iOperateAssessFunctionDao.getChild(child.getFunctionId(),1);
            if(list != null && !list.isEmpty()){
                child.setHaveChild("true");
            }
        });
        return roots;
    }

    /**
     * <desc>
     *      更改菜单状态
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价菜单DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/9
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer updateStatus(OperateAssessFunctionDTO operateAssessFunctionDTO) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("functionId",operateAssessFunctionDTO.getFunctionId());
        params.put("loginUserId",operateAssessFunctionDTO.getLoginUserId());
        params.put("status",operateAssessFunctionDTO.getStatus());
        params.put("updateTime",new Date());
        if (iOperateAssessFunctionDao.updateStatus(params) >0){
            return 1;
        }
        return 0;
    }

    /**
     * <desc>
     *      保存操作
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/9
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer save(OperateAssessFunctionDTO operateAssessFunctionDTO) throws Exception {
        Date nowTime = new Date();
        AssessFunctionPO assessFunctionPO = new AssessFunctionPO();
        BeanUtils.copyProperties(operateAssessFunctionDTO,assessFunctionPO);
        assessFunctionPO.setFunctionId(LogicIdUtil.bussinessId());
        assessFunctionPO.setCreateTime(nowTime);
        assessFunctionPO.setUpdateTime(nowTime);
        if(assessFunctionPO.getParent().equals("0")){
            if(assessFunctionPO.getFunctionUrl().equals("")){
                assessFunctionPO.setFunctionUrl("/");
            }
            if(assessFunctionPO.getIdentifier().equals("")){
                assessFunctionPO.setIdentifier("home");
            }
        }
        assessFunctionPO.setCreateUserId(operateAssessFunctionDTO.getLoginUserId());
        assessFunctionPO.setOperateUserId(operateAssessFunctionDTO.getLoginUserId());
        return iOperateAssessFunctionDao.save(SqlUtil.durableData(assessFunctionPO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      更新操作
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/9
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer update(OperateAssessFunctionDTO operateAssessFunctionDTO) throws Exception {
        Date nowTime = new Date();
        AssessFunctionPO assessFunctionPO = new AssessFunctionPO();
        BeanUtils.copyProperties(operateAssessFunctionDTO,assessFunctionPO);
        assessFunctionPO.setUpdateTime(nowTime);
        assessFunctionPO.setOperateUserId(operateAssessFunctionDTO.getLoginUserId());
        return iOperateAssessFunctionDao.update(SqlUtil.durableData(assessFunctionPO, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      删除菜单
     * </desc>
     *
     * @param operateAssessFunctionDTO 报价菜单DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/16
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer delete(OperateAssessFunctionDTO operateAssessFunctionDTO) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("functionIds",StringHandlerUtil.splitString(operateAssessFunctionDTO.getFunctionIds()));
        params.put("updateTime",new Date());
        params.put("loginUserId",operateAssessFunctionDTO.getLoginUserId());
        return iOperateAssessFunctionDao.delete(params);
    }

    /**
     * <desc>
     *      获取当前节点所有同级节点
     * </desc>
     *
     * @param parent
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/16
     */
    @Override
    public List<Map<String, Object>> getParentLevelFunctions(String parent) throws Exception {
        Map<String, Object> resultMap = iOperateAssessFunctionDao.getParent(parent);
        if (resultMap != null && !resultMap.isEmpty()){
            List<Map<String, Object>> resultList = iOperateAssessFunctionDao.getParentLevelFunctions(resultMap.get("parent")+"");
            return resultList;
        }
        return null;
    }

    /**
     * <desc>
     *      获取角色菜单
     * </desc>
     *
     * @param roleId 角色id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/22
     */
    @Override
    public List<OperateGetRoleMenuVO> getRoleMenus(String roleId) {
        List<OperateGetRoleMenuVO> returnVOS = new ArrayList<>();
        List<OperateAssessFunctionVO> functionVOS = iOperateAssessFunctionDao.getRoleMenus(roleId);
        //找到第一级节点
        List<OperateAssessFunctionVO> parentVOS = functionVOS.stream().filter(item-> "0".equals(item.getParent())).collect(Collectors.toList());
        parentVOS.forEach(item -> {
            OperateGetRoleMenuVO operateGetRoleMenuVO = new OperateGetRoleMenuVO();
            operateGetRoleMenuVO.setFunctionId(item.getFunctionId());
            operateGetRoleMenuVO.setName(item.getFunctionName());
            operateGetRoleMenuVO.setPath(item.getFunctionUrl().equals("") ? "/" : item.getFunctionUrl());
            operateGetRoleMenuVO.setComponent(item.getIdentifier().equals("") ? "home" : item.getIdentifier());
            operateGetRoleMenuVO.setOrders(item.getOrders());
            operateGetRoleMenuVO.setIconCls(item.getIcon());
            List<OperateGetRoleMenuVO> childrenVOList = getChildrenList(functionVOS,item.getFunctionId());
            if (childrenVOList != null && !childrenVOList.isEmpty()){
                operateGetRoleMenuVO.setLeaf("false");
                operateGetRoleMenuVO.setHidden("false");
            }
            else {
                operateGetRoleMenuVO.setLeaf("true");
                operateGetRoleMenuVO.setHidden("true");
            }
            operateGetRoleMenuVO.setChildren(childrenVOList);
            returnVOS.add(operateGetRoleMenuVO);

        });
        return returnVOS;
    }

    /**
     * <desc>
     *      获取子菜单列表
     * </desc>
     *
     * @param functionVOS 菜单列表
     * @param roleId 角色id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/6/22
     */
    private List<OperateGetRoleMenuVO> getChildrenList(List<OperateAssessFunctionVO> functionVOS, String roleId){
        List<OperateGetRoleMenuVO> childrenVO = new ArrayList<>();
        functionVOS.forEach(item -> {
            if (roleId.equals(item.getParent())){
                OperateGetRoleMenuVO operateGetRoleMenuVO = new OperateGetRoleMenuVO();
                operateGetRoleMenuVO.setFunctionId(item.getFunctionId());
                operateGetRoleMenuVO.setName(item.getFunctionName());
                operateGetRoleMenuVO.setPath(item.getFunctionUrl().equals("") ? "/" : item.getFunctionUrl());
                operateGetRoleMenuVO.setComponent(item.getIdentifier().equals("") ? "home" : item.getIdentifier());
                operateGetRoleMenuVO.setOrders(item.getOrders());
                operateGetRoleMenuVO.setIconCls(item.getIcon());
                List<OperateGetRoleMenuVO> childrenVOList = getChildrenList(functionVOS,item.getFunctionId());
                if (childrenVOList != null && !childrenVOList.isEmpty()){
                    operateGetRoleMenuVO.setLeaf("false");
                    operateGetRoleMenuVO.setHidden("false");
                }
                else {
                    operateGetRoleMenuVO.setLeaf("true");
                    operateGetRoleMenuVO.setHidden("true");
                }

                operateGetRoleMenuVO.setChildren(childrenVOList);
                childrenVO.add(operateGetRoleMenuVO);
            }
        });
        return childrenVO;
    }
}
